import exhibition from "../../api/exhibition";

// pages/reservation/Resvervation.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    exhibitionName: [],
    selectExhibition: '',
    exhibitionDate: [],
    exhibitionDetails: {},
    selectedId: null,
    show: false,
    minDate: new Date().setHours(0, 0, 0, 0), // 今天0点
    maxDate: new Date().setDate(new Date().getDate() + 14) // 今天+14天
  },
  // 点击事件处理函数
  selectItem(e) {
    const itemName = e.currentTarget.dataset.name;

    // 如果点击的是已选中的项，则取消选中；否则选中该项
    const newSelectedId = this.data.selectedId === itemName ? null : itemName;

    this.setData({
      selectedId: newSelectedId,
      selectExhibition: newSelectedId ? itemName : ''
    });
    console.log(this.data.selectExhibition)
    if (this.data.selectExhibition != '') {
      exhibition.getExhitionTicketOrder(this.data.selectExhibition, this.data.date).then(res => {
        this.setData({
          exhibitionDate: res.data,
          exhibitionDetails: res.detail
        })
        console.log(this.data.exhibitionDetails)
      }).catch(err => {
        console.log(err)
      })

    }
  },

  selectItemDate(e) {
    const itemDate = e.currentTarget.dataset.date;
    // 如果点击的是已选中的项，则取消选中；否则选中该项
    const newSelectedDate = this.data.selectedDate === itemDate ? null : itemDate;
    this.setData({
      selectedDate: newSelectedDate,
    });
  },

  handleChildReservation: function () {
    wx.setStorage({
      key: 'tempReservationData',
      data: this.data.exhibitionDetails,
      success: () => {
        wx.navigateTo({
          url: '/pages/ReservationConfirmation/ResvaConfirmation'
        })
      }
    })
  },

  onDisplay() {
    this.setData({
      show: true,
      // 每次打开时更新日期范围（确保动态计算）
      minDate: new Date().setHours(0, 0, 0, 0),
      maxDate: new Date().setDate(new Date().getDate() + 14)
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  // 添加 padZero 方法到 Page 对象
  padZero(num) {
    return num.toString().padStart(2, '0');
  },
  formatDate(date) {
    // 确保传入的是Date对象
    const d = new Date(date);
    const year = d.getFullYear();
    const month = d.getMonth() + 1; // 月份从0开始需+1
    const day = d.getDate();

    // 补零函数
    //const padZero = num => num.toString().padStart(2, '0');
    return `${this.padZero(year)}-${this.padZero(month)}-${this.padZero(day)}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
    console.log(this.data.date)
    exhibition.getExhibitionTicket(this.data.date).then(res => {
      this.setData({
        exhibitionName: res.data
      })
      console.log(this.data.exhibitionName)
    }).catch(err => {
      console.log(err)
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})